package com.example.loginproject.view.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import com.example.loginproject.databinding.FragmentPatternRegisterBinding
import com.example.loginproject.tool.navigateTo
import com.example.loginproject.view.viewModels.SharedViewModel

class PatternRegisterFragment : Fragment() {
    private lateinit var binding: FragmentPatternRegisterBinding
    private var mPassword = ""
    private val viewModel:SharedViewModel by activityViewModels()
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentPatternRegisterBinding.inflate(layoutInflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.unlockView.addDrawFinishedListener { unlockView, s ->
            if(mPassword.isEmpty()){
                mPassword=s
            }else{
                if(mPassword==s){
                    viewModel.user.pattern = mPassword
                    viewModel.registUser()
                   navigateTo(PinLoginFragment())
                }else{
                    mPassword = ""
                    binding.unlockView.showError()
                }
            }
        }
    }
}